package com.qf.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qf.conmmon.R;
import com.qf.entity.Permission;
import com.qf.service.IPermissionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * @propram: IntelliJ IDEA
 * @description: 权限角色控制器
 * @author: 秦先发
 * @create: 2021-09-26 16:18
 **/
@RestController
@RequestMapping("/permission")
public class PermissionController {
    @Autowired
    private IPermissionService permissionService;

    /**
     * 添加权限数据
     * @param permission
     * @return
     */
    @PostMapping("/addPermission")
    public R addPermission(@RequestBody Permission permission){
        return R.getR(permissionService.save(permission));
    }

    /**
     * 删除权限数据
     * @param id
     * @return
     */
    @GetMapping("/deletePermissionById")
    public R deletePermissionById(@RequestParam Integer id){

        return R.getR(permissionService.removeById(id));
    }

    /**
     * 更新用户数据
     * @param permission
     * @return
     */
    @PostMapping("/updatePermission")
    public R updatePermission(@RequestBody Permission permission){
        return R.getR(permissionService.updateById(permission));
    }

    /**
     * 分页查询权限数据
     */
    @PostMapping("/selectPermissionPage")
    public R selectPermissionPage(@RequestBody Page<Permission> page){
       return R.getR(permissionService.page(page));
    }


    /**
     * 批量删除
     */
    @PostMapping("/deletePermissions")
    public R deletePermissions(@RequestBody List<Integer> ids){
        int i = permissionService.getBaseMapper().deleteBatchIds(ids);
        R r = R.getR(i);
        return r;
    }

    /**
     * 查所有
     * @return
     */
    @PostMapping("/selectAllPermission")
    public R selectAllPermission(){
        List<Permission> list = permissionService.list();
       return R.saveR("data", list);
    }
}
